//
//  DBManager.m
//  FetionHD
//
//  Created by 李 俊杰 on 13-8-12.
//  Copyright (c) 2013年 chinasofti. All rights reserved.

#import <Foundation/Foundation.h>
#import "DBManager.h"

static FMDatabase *shareDataBase = nil;
@implementation DBManager

/**
 创建数据库类的单例对象
 
 **/
//+ (FMDatabase *)createDataBase {
//    //debugMethod();
//    @synchronized (self) {
//        if (shareDataBase == nil) {
//
//            shareDataBase = [[FMDatabase databaseWithPath:dataBasePath] retain];
//        }
//        return shareDataBase;
//    }
//}
//这种方法可以达到线程安全，但多次调用时会导致性能显著下降

+ (FMDatabase *)createDataBase {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        shareDataBase = [FMDatabase databaseWithPath:dataBasePath];
    });
    return shareDataBase;
}

/**
 判断数据库中表是否存在
 **/
+ (BOOL) isTableExist:(NSString *)tableName
{
    FMResultSet *rs = [shareDataBase executeQuery:@"select count(*) as 'count' from sqlite_master where type ='table' and name = ?", tableName];
    while ([rs next])
    {
        // just print out what we've got in a number of formats.
        NSInteger count = [rs intForColumn:@"count"];
        NSLog(@"%@ isOK %ld", tableName,count);
        
        if (0 == count)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    
    return NO;
}

/**
 创建表
 **/
+ (BOOL)createTable {
    debugMethod();
    NSLog(@"%@",dataBasePath);
    if (1){
        {
            shareDataBase = [DBManager createDataBase];
            if ([shareDataBase open]) {
                if (![DBManager isTableExist:@"park_table"]) {
                    NSString *sql = @"CREATE TABLE \"park_table\" (\"park_id\" TEXT PRIMARY KEY  NOT NULL  check(typeof(\"park_id\") = 'text') , \"parkEntity\" BLOB)";
                    NSLog(@"no Medicine ");
                    [shareDataBase executeUpdate:sql];
                }
                [shareDataBase close];
            }
        }
    }
    return YES;
}

/**
 关闭数据库
 **/
+ (void)closeDataBase {
    if(![shareDataBase close]) {
        NSLog(@"数据库关闭异常，请检查");
        return;
    }
}

/**
 删除数据库
 **/
+ (void)deleteDataBase {
    if (shareDataBase != nil) {
        //这里进行数据库表的删除工作
    }
}

+ (BOOL) saveOrUpdataParkEntity:(ParkEntity*)parkEntity
{
    BOOL isOk = NO;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        isOk = [shareDataBase executeUpdate:
                @"INSERT INTO \"park_table\" (\"park_id\",\"parkEntity\") VALUES(?,?)",parkEntity.ID,[NSKeyedArchiver archivedDataWithRootObject:parkEntity]];
        [shareDataBase close];
    }
    return isOk;
}
+(BOOL)UpdataParkEntity:(ParkEntity*)parkEntity
{
    BOOL isOk = NO;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        isOk = [shareDataBase executeUpdate:
                @"UPDATE park_table SET parkEntity = ? WHERE park_id = ?",[NSKeyedArchiver archivedDataWithRootObject:parkEntity],parkEntity.ID];
        [shareDataBase close];
    }
    return isOk;
}
+ (ParkEntity *) selectParkByParkId:(NSString*)parkId
{
    ParkEntity *m = nil;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM \"park_table\" WHERE \"park_id\" = '%@'",parkId]];
        if ([s next]) {
            m = [[ParkEntity alloc] init];
            m.ID = [s stringForColumn:@"park_id"];
            m = [NSKeyedUnarchiver unarchiveObjectWithData:[s dataForColumn:@"parkEntity"]];
        }
        [shareDataBase close];
    }
    return m;
}

+ (NSMutableArray *) selectAllDownloadPark
{
    NSMutableArray *mutableArrayPark = [[NSMutableArray alloc] init];
    
    ParkEntity *m = nil;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        FMResultSet *s = [shareDataBase executeQuery:[NSString stringWithFormat:@"SELECT * FROM \"park_table\""]];
        while ([s next]) {
            m = [[ParkEntity alloc] init];
            m.ID = [s stringForColumn:@"park_id"];
            m = [NSKeyedUnarchiver unarchiveObjectWithData:[s dataForColumn:@"parkEntity"]];
            
            [mutableArrayPark addObject:m];
        }
        [shareDataBase close];
    }
    return mutableArrayPark;
}

+ (BOOL) delegateItem:(ParkEntity*)parkEntity
{
    //DELETE FROM Person WHERE LastName = 'Wilson'
    BOOL isOk = NO;
    shareDataBase = [DBManager createDataBase];
    if ([shareDataBase open]) {
        isOk = [shareDataBase executeUpdate:[NSString stringWithFormat:@"DELETE FROM \"park_table\" WHERE \"park_id\" = '%@'",parkEntity.ID]];
        [shareDataBase close];
    }
    return isOk;
}

@end


